<?php

session_start();
include ('../Fonctions.php');

$infoConvention = array(
    $commune = trim(htmlspecialchars(strip_tags($_POST['nomCommune06']))),
    $departement = trim(htmlspecialchars(strip_tags($_POST['departement06']))),
    $nomLigne = trim(htmlspecialchars(strip_tags($_POST['nomLigne06']))),
    $civilite = trim(htmlspecialchars(strip_tags($_POST['civilite06']))),
    $nomProprio = trim(htmlspecialchars(strip_tags($_POST['nomProprio06']))),
    $addrProprio = trim(htmlspecialchars(strip_tags($_POST['adresseProprio06']))),
    $addrTerrain = trim(htmlspecialchars(strip_tags($_POST['adresseTerrain06']))),
    $exploite = trim(htmlspecialchars(strip_tags($_POST['exploitation06']))),
    $nomExterne = trim(htmlspecialchars(strip_tags($_POST['exploitant06']))),
    $addrExterne = trim(htmlspecialchars(strip_tags($_POST['addrExploitant06']))),
    $nbrSupports = trim(htmlspecialchars(strip_tags($_POST['nbrSupports06']))),
    $nbrAncrages = trim(htmlspecialchars(strip_tags($_POST['nbrAncrages06']))),
    $longueurTotale = trim(htmlspecialchars(strip_tags($_POST['longeurTotale06']))),
    $longueurFacade = trim(htmlspecialchars(strip_tags($_POST['longeurFacade06']))),
    $sommeChiffres = trim(htmlspecialchars(strip_tags($_POST['sommeChiffres06']))),
    $sommeLettres = trim(htmlspecialchars(strip_tags($_POST['sommeLettres06']))),
    $nomNotaire = trim(htmlspecialchars(strip_tags($_POST['nomNotaire06']))),
    $addrNotaire = trim(htmlspecialchars(strip_tags($_POST['addrNotaire06']))),
    $lieuProprio = trim(htmlspecialchars(strip_tags($_POST['lieuProprio06']))),
    $dateProprio = trim(htmlspecialchars(strip_tags($_POST['dateC06Proprio']))),
    $lieuERDF = trim(htmlspecialchars(strip_tags($_POST['lieuERDF06']))),
    $dateERDF = trim(htmlspecialchars(strip_tags($_POST['dateC06ERDF']))),
    $pdf = trim(htmlspecialchars(strip_tags($_POST['valider']))));

foreach ($infoConvention as $cle => $valeur) {
    if ($infoConvention[$cle] == '')
        $infoConvention[$cle] = ".....................";
    echo "clé: " . $cle . " valeur: " . $infoConvention[$cle] . "<br />";
}

if (isset($_POST['support1'])) {
    $supports = trim(htmlspecialchars(strip_tags($_POST['support1'])));
} else {
    $supports = "null";
}

$i = 2;
while (isset($_POST['support' . $i])) {
    ${"support" . $i} = trim(htmlspecialchars(strip_tags($_POST['support' . $i])));
    $supports .= "_" . ${"support" . $i};
    echo ${"support" . $i} . "<br />   ";
    $i++;
}
echo $supports;

if (isset($_POST['communeC06_1'])) {
    $communeParcelle = trim(htmlspecialchars(strip_tags($_POST['communeC06_1'])));
    $sectionParcelle = trim(htmlspecialchars(strip_tags($_POST['sectionC06_1'])));
    $numeroParcelle = trim(htmlspecialchars(strip_tags($_POST['numeroC06_1'])));
    $lieuxParcelle = trim(htmlspecialchars(strip_tags($_POST['lieuxC06_1'])));
    $natureParcelle = trim(htmlspecialchars(strip_tags($_POST['natureC06_1'])));
    if ($lieuxParcelle == '')
        $lieuxParcelle = ".....................";
    if ($natureParcelle == '')
        $natureParcelle = ".....................";
}

$i = 2;
while (isset($_POST['communeC06_' . $i])) {
    $tempLieux = trim(htmlspecialchars(strip_tags($_POST['lieuxC06_' . $i])));
    $tempNature = trim(htmlspecialchars(strip_tags($_POST['natureC06_' . $i])));
    if ($tempLieux == '')
        $tempLieux = ".....................";
    if ($tempNature == '')
        $tempNature = ".....................";
    $communeParcelle .= '_' . trim(htmlspecialchars(strip_tags($_POST['communeC06_' . $i])));
    $sectionParcelle .= '_' . trim(htmlspecialchars(strip_tags($_POST['sectionC06_' . $i])));
    $numeroParcelle .= '_' . trim(htmlspecialchars(strip_tags($_POST['numeroC06_' . $i])));
    $lieuxParcelle .= '_' . $tempLieux;
    $natureParcelle .= '_' . $tempNature;
    $i++;
}

echo $communeParcelle . "<br />";
echo $sectionParcelle . "<br />";
echo $numeroParcelle . "<br />";
echo $lieuxParcelle . "<br />";
echo $natureParcelle . "<br />";

//insertion en bdd
$insertionC06->execute(array(
    'type_etude' => "municipalite",
    'id_etude' => $_SESSION['id_etude'],
    'nom_convention' => 'C06',
    'nom_commune' => $commune,
    'departement' => $departement,
    'nom_ligne' => $nomLigne,
    'civilite' => $infoConvention[3],
    'proprio' => $infoConvention[4],
    'adresse_proprio' => $infoConvention[5],
    'adresse_terrain' => $addrTerrain,
    'commune' => $communeParcelle,
    'section' => $sectionParcelle,
    'numero' => $numeroParcelle,
    'lieux' => $lieuxParcelle,
    'nature' => $natureParcelle,
    'exploitation' => $infoConvention[7],
    'adresse_exploitant' => $infoConvention[8],
    'exploitant' => $infoConvention[9],
    'nbr_supports' => $nbrSupports,
    'nbr_ancrages' => $nbrAncrages,
    'dimensions_supports' => $supports,
    'longueur_totale' => $longueurTotale,
    'longueur_facade' => $longueurFacade,
    'somme_chiffres' => $sommeChiffres,
    'somme_lettres' => $sommeLettres,
    'adresse_notaire' => $addrNotaire,
    'nom_notaire' => $nomNotaire,
    'lieu_proprio' => $infoConvention[18],
    'date_proprio' => $infoConvention[19],
    'lieu_erdf' => $infoConvention[20],
    'date_erdf' => $infoConvention[21]
));

if ($pdf == "PDF/") {
    $infosTemp = $bdd->prepare("SELECT id_convention_c06 FROM convention_c06 WHERE type_etude = :type_etude AND id_etude = :id_etude AND nom_commune = :nom_commune AND departement = :departement AND nom_ligne = :nom_ligne 
                AND civilite = :civilite AND  proprio = :proprio AND adresse_proprio =:adresse_proprio  AND adresse_terrain = :adresse_terrain AND commune = :commune AND section = :section
                AND numero = :numero AND lieux = :lieux AND nature = :nature AND exploitation = :exploitation AND adresse_exploitant = :adresse_exploitant AND exploitant = :exploitant
                AND nbr_supports = :nbr_supports AND nbr_ancrages = :nbr_ancrages AND dimensions_supports = :dimensions_supports AND longueur_totale = :longueur_totale AND longueur_facade = :longueur_facade AND somme_chiffres = :somme_chiffres AND 
                somme_lettres = :somme_lettres AND adresse_notaire = :adresse_notaire AND nom_notaire = :nom_notaire AND lieu_proprio = :lieu_proprio AND date_proprio = :date_proprio AND lieu_erdf = :lieu_erdf AND date_erdf = :date_erdf");

    $infosTemp->execute(array(
        'type_etude' => "municipalite",
        'id_etude' => $_SESSION['id_etude'],
        'nom_commune' => $commune,
        'departement' => $departement,
        'nom_ligne' => $nomLigne,
        'civilite' => $infoConvention[3],
        'proprio' => $infoConvention[4],
        'adresse_proprio' => $infoConvention[5],
        'adresse_terrain' => $addrTerrain,
        'commune' => $communeParcelle,
        'section' => $sectionParcelle,
        'numero' => $numeroParcelle,
        'lieux' => $lieuxParcelle,
        'nature' => $natureParcelle,
        'exploitation' => $infoConvention[7],
        'adresse_exploitant' => $infoConvention[8],
        'exploitant' => $infoConvention[9],
        'nbr_supports' => $nbrSupports,
        'nbr_ancrages' => $nbrAncrages,
        'dimensions_supports' => $supports,
        'longueur_totale' => $longueurTotale,
        'longueur_facade' => $longueurFacade,
        'somme_chiffres' => $sommeChiffres,
        'somme_lettres' => $sommeLettres,
        'adresse_notaire' => $addrNotaire,
        'nom_notaire' => $nomNotaire,
        'lieu_proprio' => $infoConvention[18],
        'date_proprio' => $infoConvention[19],
        'lieu_erdf' => $infoConvention[20],
        'date_erdf' => $infoConvention[21]
    ));

    $temp = $infosTemp->fetch();

    header('Location: ../conventionC06.php?typeEtude=municipalite&idEtude=' . $_SESSION['id_etude'] . '&idConvention=' . $temp['id_convention_c06'] . '&pdf=oui');
}
?>
